package ee.serge.client.content.cp;

import java.util.ArrayList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;

import ee.serge.client.AlertBox;
import ee.serge.client.Move;
import ee.serge.client.content.cp.article.ArticlesContent;
import ee.serge.service.Service;
import ee.serge.shared.Article;
import ee.serge.shared.Feedback;
import ee.serge.shared.Loan;
import ee.serge.util.AdminContent;
import ee.serge.util.SimpleCallback;
import ee.serge.util.URLData;
import ee.serge.util.bootstrap.BButton;
import ee.serge.util.bootstrap.BButtonBar;

public class AutomatedTestingContent extends AdminContent {

private static final String NAME = "cp/automated_testing";
   
   public static String getName() {
      return NAME;
   }
   
   public static URLData getURLData() {
      URLData data = new URLData();
      data.setName(NAME);
      return data;
   }
   
   public static void show() {
      Move.get().setContent(new AutomatedTestingContent());
   }
   
   public AutomatedTestingContent() {
      setHeader("Automated testing", "Generate database entries");
      BButtonBar buttonBar = new BButtonBar();
      BButton loanButton = new BButton("Loans x10", BButton.PRIMARY, new ClickHandler() {
         
         @Override
         public void onClick(ClickEvent event) {
            for (int i = 0; i < 10; i++) {
               Service.getControlPanelService().addLoan(new Loan(
                     "Loan" + i,
                     "www.loan" + i + ".com",
                     "An offbeat romantic comedy about a woman who doesn't believe true love exists, and the young man who falls for her.",
                     (int)(Math.random() * 50),
                     (int)(50 + Math.random() * 500),
                     (int)(50 + Math.random() * 500),
                     (int)(Math.random() * 10),
                     (int)(Math.random() * 5),
                     Math.random() > .5 ? 30 : 60,
                     true,
                     "http://media.smashingmagazine.com/images/logos/fygo.gif",
                     "www.loan" + i + ".com/screen.jpg",
                     0,
                     0,
                     new ArrayList<Feedback>()), new SimpleCallback<Void>() {

                        @Override
                        public void success(Void result) {
                           AlertBox.showSuccess("Added loan");
                        }
                     });
            }
         }
      });
      buttonBar.add(loanButton);
      
      BButton newArticleButton = new BButton("New [Know your rights]", BButton.PRIMARY, new ClickHandler() {
         
         @Override
         public void onClick(ClickEvent event) {
            Service.getArticleService().addArticle(new Article(ArticlesContent.TYPE_LOANERS_RIGHTS, "lalalal"), new SimpleCallback<Void>() {

               @Override
               public void success(Void result) {
                  AlertBox.showSuccess("Added article");
               }
            });
         }
      });      
      buttonBar.add(newArticleButton);
      
      BButton newArticleButton2 = new BButton("New [Choosing a lender]", BButton.PRIMARY, new ClickHandler() {
         
         @Override
         public void onClick(ClickEvent event) {
            Service.getArticleService().addArticle(new Article(ArticlesContent.TYPE_HOW_TO_CHOOSE_A_LOAN, "lalalal"), new SimpleCallback<Void>() {

               @Override
               public void success(Void result) {
                  AlertBox.showSuccess("Added article");
               }
            });
         }
      });      
      buttonBar.add(newArticleButton2);
      
      BButton newArticleButton3 = new BButton("New [Contact]", BButton.PRIMARY, new ClickHandler() {
         
         @Override
         public void onClick(ClickEvent event) {
            Service.getArticleService().addArticle(new Article(ArticlesContent.TYPE_CONTACT, "lalalal"), new SimpleCallback<Void>() {

               @Override
               public void success(Void result) {
                  AlertBox.showSuccess("Added article");
               }
            });
         }
      });      
      buttonBar.add(newArticleButton3);
      
      add(buttonBar);
      
      BButton addReview = new BButton("Add default review", BButton.PRIMARY, new ClickHandler() {

         @Override
         public void onClick(ClickEvent event) {
            Service.getControlPanelService().addFeedback(new Feedback("", 0, "DO NOT REMOVE THIS", 0, ""), new SimpleCallback<Void>() {

               @Override
               public void success(Void result) {
                  AlertBox.showSuccess("Review added");
               }
            });
         }
         
      });
         
         
      buttonBar.add(addReview);
      
      add(buttonBar);
   }
   
}
